package com.hxb.library.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.widget.TextView;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/16
 *     desc  : 字符串相关工具类
 * </pre>
 */
public class StringUtils {

    private static Context mContext;

    public static void init(Context context) {
        mContext = context;
    }

    private StringUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 判断字符串是否为null或长度为0
     *
     * @param s 待校验字符串
     * @return {@code true}: 空<br> {@code false}: 不为空
     */
    public static boolean isEmpty(CharSequence s) {
        return s == null || s.length() == 0 || s.equals("null") || s.equals("NULL");
    }

    /**
     * 判断字符串是否为null或长度为0
     *
     * @param s 待校验字符串
     * @return {@code true}: 空<br> {@code false}: 不为空
     */
    public static boolean isNoEmpty(CharSequence s) {
        return !isEmpty(s);
    }

    /**
     * 判断字符串是否为null或全为空格
     *
     * @param s 待校验字符串
     * @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
     */
    public static boolean isSpace(String s) {
        return (s == null || s.trim().length() == 0);
    }

    /**
     * 判断两字符串是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 判断两字符串忽略大小写是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    public static boolean equalsIgnoreCase(String a, String b) {
        return (a == b) || (b != null) && (a.length() == b.length()) && a.regionMatches(true, 0, b, 0, b.length());
    }

    /**
     * null转为长度为0的字符串
     *
     * @param s 待转字符串
     * @return s为null转为长度为0字符串，否则不改变
     */
    public static String null2Length0(String s) {
        return s == null ? "" : s;
    }

    /**
     * 返回字符串长度
     *
     * @param s 字符串
     * @return null返回0，其他返回自身长度
     */
    public static int length(CharSequence s) {
        return s == null ? 0 : s.length();
    }

    /**
     * 首字母大写
     *
     * @param s 待转字符串
     * @return 首字母大写字符串
     */
    public static String upperFirstLetter(String s) {
        if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s;
        return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
    }

    /**
     * 首字母小写
     *
     * @param s 待转字符串
     * @return 首字母小写字符串
     */
    public static String lowerFirstLetter(String s) {
        if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) return s;
        return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
    }

    /**
     * 反转字符串
     *
     * @param s 待反转字符串
     * @return 反转字符串
     */
    public static String reverse(String s) {
        int len = length(s);
        if (len <= 1) return s;
        int mid = len >> 1;
        char[] chars = s.toCharArray();
        char c;
        for (int i = 0; i < mid; ++i) {
            c = chars[i];
            chars[i] = chars[len - i - 1];
            chars[len - i - 1] = c;
        }
        return new String(chars);
    }

    /**
     * 转化为半角字符
     *
     * @param s 待转字符串
     * @return 半角字符串
     */
    public static String toDBC(String s) {
        if (isEmpty(s)) return s;
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == 12288) {
                chars[i] = ' ';
            } else if (65281 <= chars[i] && chars[i] <= 65374) {
                chars[i] = (char) (chars[i] - 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    /**
     * 转化为全角字符
     *
     * @param s 待转字符串
     * @return 全角字符串
     */
    public static String toSBC(String s) {
        if (isEmpty(s)) return s;
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == ' ') {
                chars[i] = (char) 12288;
            } else if (33 <= chars[i] && chars[i] <= 126) {
                chars[i] = (char) (chars[i] + 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    /**
     * @作者 ：hexingbo
     * @时间 ：2019/3/19
     * @描述 ：手机号码格式化微182****5716
     */
    public static String phoneNumberFormat(String phone) {
        if (!TextUtils.isEmpty(phone) && phone.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < phone.length(); i++) {
                char c = phone.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
        return phone;
    }

    /**
     * 银行卡号格式化微6217007200006091295---》************1295
     *
     * @param start         *开始位置
     * @param showEndLength 末尾显示几位
     * @param number        卡号
     * @return
     */
    public static String bankNumberFormat(int start, int showEndLength, String number) {
        if (!TextUtils.isEmpty(number) && number.length() > 7) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < number.length(); i++) {
                char c = number.charAt(i);
                if (i == start && i <= number.length() - showEndLength - 1) {//保留后四位
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }

            return (sb.toString());
        } else {
            return "";
        }
    }

    /**
     * 金额添加￥符号，未作任何处理
     *
     * @param view
     * @param str
     */
    public static void setMoneyDefault(TextView view, String str) {
        String split = "";
        if (TextUtils.isEmpty(str)) {
            split = "¥" + 0.00;
        } else {
            split = "¥" + str;
        }
        view.setText(split);
    }

    /**
     * 金额添加￥符号，整数位大写，小数位小写
     *
     * @param view
     * @param str
     */
    public static void setMoneyIntBig(TextView view, String str) {
        String split = "";
        if (TextUtils.isEmpty(str)) {
            split = "¥" + "<font><big>" + 0 + "</big></font>" + ".00";
        } else {
            if (str.contains(".")) {
                split = "¥" + "<font><big>" + str.substring(0, str.indexOf("."))
                        + "</big></font>" + str.substring(str.indexOf("."), str.length());
            } else {
                split = "¥" + "<font><big>" + str + "</big></font>";
            }

        }
        view.setText(Html.fromHtml(split));
    }

    /**
     * 金额添加￥符号，全部大写
     *
     * @param view
     * @param str
     */
    public static void setMoneyDefaultBig(TextView view, String str) {
        String split = "";
        if (TextUtils.isEmpty(str)) {
            split = "¥" + "<font><big>" + 0.00 + "</big></font>";
        } else {
            split = "¥" + "<font><big>" + str + "</big></font>";
        }
        view.setText(Html.fromHtml(split));
    }

    /**
     * 金额添加￥符号，全部大写，并科学计数法显示保留两位小数
     *
     * @param view
     * @param str
     */
    public static void setMoneyDefaultBigFormat(TextView view, String str) {
        String split = "";
        if (TextUtils.isEmpty(str)) {
            split = "¥" + "<font><big>" + 0.00 + "</big></font>";
        } else {
            split = "¥" + "<font><big>" + NumberFormatUtil.numberFormat(Double.valueOf(str)) + "</big></font>";
        }
        view.setText(Html.fromHtml(split));
    }

    public static void setMoneyLineCenter(TextView view, String str) {
        String split = "";
        if (TextUtils.isEmpty(str)) {
            split = "¥ " + 0;
        } else {
            split = "¥" + str;
        }
        view.setText(split);
        view.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线
    }

    public static int setStingDoubleToInt(String strDouble) {
        return Double.valueOf(strDouble).intValue();
    }

    /**
     * 复制
     *
     * @param text
     */
    public static void copyTxt(String text) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", text);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }

    /**
     * 复制
     *
     * @param context
     * @param value
     */
    public static void copTextValue(Context context, String value) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", value);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }

    public static String getStringNoEmpty(String value) {
        return isEmpty(value) ? "" : value;
    }

    public static void setTextValue(TextView tv, String value) {
        tv.setText(getStringNoEmpty(value));
    }

}